package com.deng.etl.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.deng.etl.common.IEtlInterface;
import com.deng.etl.common.EtlLog;
//清空
public class ImportDepartment extends ImportBase implements IEtlInterface {
	
	public static String COLUMNS="dept_code,dept_name";
	
	public static String SELECT="select "+COLUMNS+"  from neusoft.JIXIAO_DEPARTMENT";
	
	public static String INSERT="insert into t_sys_his_department (his_dept_code,his_dept_name,create_time,modify_time) values(?,?,?,?)";
	
	public static String DELETE ="delete from t_sys_his_department";
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		
	}
	
	public void insertData(Connection sourceConn,Connection targetConn,String name,String fromDate,String toDate) {
		System.out.println("从："+fromDate+"   到："+toDate);
		
		try {
			EtlLog.progressOneTaskETLLog(targetConn, name, null);//启动日志
			PreparedStatement pst=sourceConn.prepareStatement(SELECT);
			
			PreparedStatement pst_insert=targetConn.prepareStatement(INSERT);
			ResultSet rs=pst.executeQuery();
			while(rs.next()){
				String or_code=rs.getString("dept_code");

				pst_insert.setString(1, or_code);
				pst_insert.setString(2, rs.getString("dept_name"));
				Date date=new Date();
				pst_insert.setTimestamp(3, getTimestamp(date));
				pst_insert.setTimestamp(4, getTimestamp(date));
				insert(pst_insert);
				
			}
			
			rs.close();
			pst.close();
			pst_insert.close();
			EtlLog.finishOneTaskETLLog(targetConn, name, toDate);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public void deleteData(Connection sourceConn, Connection targetConn,
			String name, String fromDate, String toDate) {
		// TODO Auto-generated method stub
		System.out.println("从："+fromDate+"   到："+toDate);
		try{
			EtlLog.progressOneTaskETLLog(targetConn, name, null);//启动日志
			PreparedStatement pst_delete=targetConn.prepareStatement(DELETE);
			pst_delete.executeUpdate();
			pst_delete.close();
				
			EtlLog.finishOneTaskETLLog(targetConn, name, toDate);
		}
		catch(SQLException e){
			
		}
		
	}

	public void updateData(Connection sourceConn, Connection targetConn,
			String name, String fromDate, String toDate) {
		try{
			EtlLog.progressOneTaskETLLog(targetConn, name, null);//启动日志
			
			deleteData(sourceConn, targetConn,
					name, fromDate, toDate);
			insertData(sourceConn, targetConn,
					name, fromDate, toDate);
			
			EtlLog.finishOneTaskETLLog(targetConn, name, toDate);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		
	}

	public boolean checkExist(Connection conn,String code){
		boolean ret=false;
		try {
			System.out.println("---------checkExist-------"+code);
			PreparedStatement pst =conn.prepareStatement("select his_dept_code from t_sys_his_department where his_dept_code='"+code+"'");
			ResultSet rs=pst.executeQuery();
			if(rs.next()){
				ret=true;
			}
			rs.close();
			pst.close();
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ret;
	}

}
